package com.cn;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;


/**
 * 老师流程审批
 */
@SpringBootTest
public class teacher_approver {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private HistoryService historyService;


    /**
     * 导入流程图老师流程
     */
    @Test
    void startTeacherApprover(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process01-基础/老师审批流程.bpmn20.xml")
                .name("teackerApprover02")
                .deploy();
        System.out.println(deploy.getId());
    }


    /**
     * 启动流程
     */
    @Test
    void startRu(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("student01","zhangsan");
        map.put("student02","lisi");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("teacker_approver", map);
        System.out.println(processInstance.getProcessDefinitionId()+"流程定义id");
        System.out.println(processInstance.getId()+"实力id");
    }

    /**
     * 通过候选人查询任务
     */
    @Test
    void queryTaskService(){
        List<Task> list = taskService.createTaskQuery().processInstanceId("47a4ff8f-ced7-11ee-a74f-30d04231d1ff")
                .taskCandidateUser("lisi")
                .list();
        for (Task task : list) {
            System.out.println(task.getId()+"id");
            System.out.println(task.getName()+"名称");
        }
    }

    /**
     * 获取任务的方式
     */
    @Test
    void gainTask(){
        Task task = taskService.createTaskQuery().processInstanceId("47a4ff8f-ced7-11ee-a74f-30d04231d1ff")
                .taskCandidateUser("zhangsan")
                .singleResult();
        if (task != null) {
            taskService.claim(task.getId(),"zhangsan");
        }
    }

    /**
     * 任务交接
     */
    @Test
    void connectTask(){
        Task task = taskService.createTaskQuery()
                .processInstanceId("47a4ff8f-ced7-11ee-a74f-30d04231d1ff")
                .singleResult();
        if (task != null) {
            taskService.setAssignee(task.getId(),"lisi");
        }
    }

    /**
     * 完成任务
     */
    @Test
    void accomplish(){
        Task task = taskService.createTaskQuery()
                .processInstanceId("47a4ff8f-ced7-11ee-a74f-30d04231d1ff")
                .singleResult();
        if (task != null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("teacher01","lilaoshi");
            map.put("teacher02","wanglaoshi");
            taskService.complete(task.getId(),map);
        }
    }

    /**
     * 候选人比较多可以放到一个组中方便维护
     */
    @Test
    void createUser(){
        User user = null;
        for (int i = 0; i < 3; i++) {
             user= identityService.newUser("student" + i);
             user.setFirstName(i+"");
             user.setEmail(i+"qq.com");
             identityService.saveUser(user);
        }
    }

    /**
     * 创建研发组信息
     * 查询 act_id_group
     */
    @Test
    void createGroup(){
        Group group1 = identityService.newGroup("group1");
        group1.setName("研发部");
        group1.setType("1");
        identityService.saveGroup(group1);
    }

    /**
     * 分配用户到用户组
     * 查询 act_id_member
     */
    @Test
    void allocationUser(){
        Group group1 = identityService.createGroupQuery().groupId("group1").singleResult();
        List<User> list = identityService.createUserQuery().list();
        for (User user : list) {
            identityService.createMembership(user.getId(),group1.getId());
        }
    }

}
